﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using EShop.Model;
using EShop.BLL;
using EShop.Common;

namespace EShop.Client
{
    public partial class frmEmployeeInfoEdit : frmBase
    {
        public frmEmployeeInfoEdit(string strEmployeeID)
        {
            InitializeComponent();
            this.strEmployeeID = strEmployeeID;
            this.lblEmployeeID.Text = this.strEmployeeID;
        }

        bool isChanged = false;
        string strEmployeeID;

        private void frmEmployeeInfoEdit_Load(object sender, EventArgs e)
        {
            EmployeeBLL oEmployeeBLL = new EmployeeBLL();
            DataTable dt = oEmployeeBLL.QueryEmployeeInfo(strEmployeeID);
            if (dt.Rows.Count > 0)
            {
                this.txtFullName.Text = dt.Rows[0]["FullName"].ToString();;
                this.cbbSex.SelectedIndex = Convert.ToInt32(dt.Rows[0]["Sex"]);
                this.txtPID.Text = dt.Rows[0]["EmployeePID"].ToString();
                this.txtBirthday.Text = ((DateTime)dt.Rows[0]["BrIthday"]).ToString("yyyy-MM-dd");
                this.cbxIsGregorianCalendar.Checked = (bool)dt.Rows[0]["IsGregorianCalendar"];
                this.rtbRemark.Text = dt.Rows[0]["Remark"].ToString();
                this.txtMobile.Text = dt.Rows[0]["Mobile"].ToString();
                this.txtPopedom.Text = dt.Rows[0]["Popedom"].ToString();
                this.txtAddress.Text = dt.Rows[0]["Address"].ToString();
                this.isChanged = false;
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
                Close();
            }
        }

        private void btnViewComplete_Click(object sender, EventArgs e)
        {
            if (isChanged==false)
            {
                this.DialogResult = DialogResult.Cancel;
            }
            else
            {
                if (MessageBox.Show("您已经修改了该页面的数据，是否需要更新到服务器？", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                { }
                else
                {
                    this.DialogResult = DialogResult.Cancel;
                }
            }
        }

        private void btnSubmitChange_Click(object sender, EventArgs e)
        {
            string strFullName = SystemLibrary.SQLQuoteName(txtFullName.Text.Trim());
            int iSex = cbbSex.SelectedIndex;
            string strPID = SystemLibrary.SQLQuoteName(txtPID.Text.Trim());
            string strBirthday = SystemLibrary.SQLQuoteName(txtBirthday.Text);
            int iIsGregorianCalendar = Convert.ToInt32(cbxIsGregorianCalendar.Checked);
            string strPopedom = SystemLibrary.SQLQuoteName(txtPopedom.Text.Trim());
            string strAddress = SystemLibrary.SQLQuoteName(txtAddress.Text.Trim());
            string strMobile = SystemLibrary.SQLQuoteName(txtMobile.Text.Trim());
            string strRemark = SystemLibrary.SQLQuoteName(rtbRemark.Text);

            #region 验证输入信息
            if (string.IsNullOrEmpty(strFullName))
            {
                MessageBox.Show("请输入员工姓名！");
                return;
            }
            if (iSex == -1)
            {
                MessageBox.Show("请选择员工性别！");
                return;
            }
            if (string.IsNullOrEmpty(strPID))
            {
                MessageBox.Show("请输入员工身份证号码！");
                return;
            }
            if (string.IsNullOrEmpty(strBirthday))
            {
                MessageBox.Show("请输入员工生日！");
                return;
            }
            try
            {
                Convert.ToDateTime(strBirthday);
            }
            catch { return; }
            if (string.IsNullOrEmpty(strPopedom))
            {
                MessageBox.Show("请输入员工籍贯！");
                return;
            }
            if (string.IsNullOrEmpty(strAddress))
            {
                MessageBox.Show("请输入员工联系地址！");
                return;
            }
            
            #endregion

            EmployeeInfo oEmployeeInfo = new EmployeeInfo();
            oEmployeeInfo.EmployeeID = this.lblEmployeeID.Text;
            oEmployeeInfo.FullName = strFullName;
            oEmployeeInfo.Sex = iSex == 1 ? true : false;
            oEmployeeInfo.EmployeePID = strPID;
            oEmployeeInfo.Brithday = Convert.ToDateTime(strBirthday); ;
            oEmployeeInfo.IsGregorianCalendar = iIsGregorianCalendar == 1 ? true : false;
            oEmployeeInfo.Popedom = strPopedom;
            oEmployeeInfo.Address = strAddress;
            oEmployeeInfo.Mobile = strMobile;
            oEmployeeInfo.Remark = strRemark;
            if (new EmployeeBLL().UpdateEmployeeInfo(oEmployeeInfo))
            {
                MessageBox.Show("修改信息成功");
                this.DialogResult = DialogResult.OK;
            }
            else 
            {
                MessageBox.Show("修改信息失败");
                this.DialogResult = DialogResult.Cancel;
            }
        }
    }
}
